package xj.graph2d.encoder;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.IOException;
import java.util.Properties;

import org.freehep.graphics2d.VectorGraphics;
import org.freehep.graphicsio.PageConstants;
import org.freehep.graphicsio.gif.GIFGraphics2D;
import org.freehep.graphicsio.ps.PSGraphics2D;

import xj.graph2d.AbstractEncoder;
import xj.graph2d.DrawCanvas;
import xj.graph2d.View;

//import org.freehep.graphicsio.java.JavaGraphics2D;

public class FreeHepEncoder extends AbstractEncoder {

  public FreeHepEncoder() {
  }

  public FreeHepEncoder(String name, String fileExtension,
      String fileDescription, int imageType, View view) {
    super(name, fileExtension, fileDescription, view);
    this.imageType = imageType;
  }

  public void setImageType(int imageType) {
    this.imageType = imageType;
  }

  @Override
  public void setImageType(String type) {
    if ("jpg".equals(type) || "jpeg".equals(type)) {
      imageTypeName = "jpeg";
      imageType = JPEG;
    } else if ("png".equals(type)) {
      imageTypeName = "png";
      imageType = PNG;
    } else if ("emf".equals(type)) {
      imageTypeName = "emf";
      imageType = EMF;
    } else if ("gif".equals(type)) {
      imageTypeName = "gif";
      imageType = GIF;
    } else if ("pdf".equals(type)) {
      imageTypeName = "pdf";
      imageType = PDF;
    } else if ("ppm".equals(type)) {
      imageTypeName = "ppm";
      imageType = PPM;
    } else if ("eps".equals(type)) {
      imageTypeName = "eps";
      imageType = EPS;
    } else if ("svg".equals(type)) {
      imageTypeName = "svg";
      imageType = SVG;
    } else if ("swf".equals(type)) {
      imageTypeName = "swf";
      imageType = SWF;
    } else {
      imageTypeName = null;
    }
  }

  @Override
  public void encode(String filename, Rectangle2D r) throws IOException {
    if (view != null) {
      DrawCanvas canvas = view.getCanvas();
      if (filename != null && imageTypeName != null && canvas != null
	  && r != null) {
	int mw = (int) view.getMarginWidth();
	int mh = (int) view.getMarginHeight();
	int w = (int) r.getWidth() + 2 * mw;
	int h = (int) r.getHeight() + 2 * mw;

	File file = new File(filename);
	Dimension dim = new Dimension(w, h);
	Properties p = new Properties();
	// p.setProperty("PageSize","A5"); ///??
	// p.setProperty("Orientation", "Portrait");
	// p.setProperty("Orientation", "Landscape");
	VectorGraphics g = null;
	if (imageType == CGM) {
	  // g = new CGMGraphics2D(file, dim);
	} else if (imageType == EMF) {
	  // g = new EMFGraphics2D(file, dim);
	} else if (imageType == JPEG) {
	  // g = new ImageGraphics2D(file, dim, "jpeg");
	} else if (imageType == GIF) {
	  g = new GIFGraphics2D(file, dim);
	  setRenderingHints(g);
	} else if (imageType == PDF) {
	  // g = new PDFGraphics2D(file, dim);
	} else if (imageType == PNG) {
	  // g = new ImageGraphics2D(file, dim, "png");
	} else if (imageType == PPM) {
	  // g = new ImageGraphics2D(file, dim, "ppm");
	} else if (imageType == EPS) {
	  g = new PSGraphics2D(file, dim);
	  // g = new PSGraphics2DExt(file, dim);
	  p.setProperty(PSGraphics2D.ORIENTATION, PageConstants.PORTRAIT);
	  p.setProperty(PSGraphics2D.FIT_TO_PAGE, "false");
	} else if (imageType == SVG) {
	  // g = new SVGGraphics2D(file, dim);
	} else if (imageType == SWF) {
	  // g = new SWFGraphics2D(file, dim);
	}
	if (g != null) {
	  g.setProperties(p);
	  g.startExport();
	  if (!transparent) {
	    Color bg = view.getBackgroundColor();
	    g.setColor(bg);
	    g.fillRect(0, 0, w, h);
	  }

	  drawWaterMark(g, 0, 0, w, h);

	  g.translate(-r.getX() + mw, -r.getY() + mh);
	  view.paintCanvas(g, null, true); // draw all objects

	  g.endExport();
	}
      }
    }
  }

  protected int imageType;

  protected String imageTypeName;

}
